﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Agnes.Core;

namespace Agnes.TaskManager
{
    class TaskPadViewModel : ScreenBase<TaskManager>
    {
        private ObservableCollection<Task> _prepareTask;
        /// <summary>
        /// 快要开始的任务
        /// </summary>
        public ObservableCollection<Task> PrepareTask
        {
            get
            {
                return _prepareTask;
            }
            set
            {
                _prepareTask = value;
                NotifyOfPropertyChange(() => PrepareTask);
            }
        }

        private ObservableCollection<Task> _progressTask;
        /// <summary>
        /// 正在进行的任务
        /// </summary>
        public ObservableCollection<Task> ProgressTask
        {
            get
            {
                return _progressTask;
            }
            set
            {
                _progressTask = value;
                NotifyOfPropertyChange(() => ProgressTask);
            }
        }

        private ObservableCollection<Task> _finishingTask;
        /// <summary>
        /// 快要结束的任务
        /// </summary>
        public ObservableCollection<Task> FinishingTask
        {
            get
            {
                return _finishingTask;
            }
            set
            {
                _finishingTask = value;
                NotifyOfPropertyChange(() => FinishingTask);
            }
        }

        private ObservableCollection<Task> _outOfDateTask;
        /// <summary>
        /// 已经过期任务
        /// </summary>
        public ObservableCollection<Task> OutofDateTask
        {
            get
            {
                return _outOfDateTask;
            }
            set
            {
                _outOfDateTask = value;
                NotifyOfPropertyChange(() => OutofDateTask);
            }
        }

        private int _type = 0;

        public int Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
                RefleshData(value);
                NotifyOfPropertyChange(() => Type);
            }
        }

        public TaskPadViewModel(TaskManager model)
            : base()
        {
            PrepareTask = new ObservableCollection<Task>();
            ProgressTask = new ObservableCollection<Task>();
            FinishingTask = new ObservableCollection<Task>();
            OutofDateTask = new ObservableCollection<Task>();
            Model = model;
        }

        protected override void OnModelChanged(TaskManager oldValue, TaskManager newValue)
        {
            base.OnModelChanged(oldValue, newValue);
            if (Model != null)
            {
                foreach (Task i in Model.LeafTasks)
                {
                    i.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(i_PropertyChanged);
                }
                Model.LeafTasks.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(LeafTasks_CollectionChanged);
            }
            Type = 0;
        }

        void LeafTasks_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
            {
                foreach (Task i in e.NewItems)
                {
                    i.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(i_PropertyChanged);
                }
            }
            if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
            {
                foreach (Task i in e.OldItems)
                {
                    i.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(i_PropertyChanged);
                }
            }
            RefleshData(Type);
        }

        void i_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            RefleshData(Type);
        }

        protected override object CreateView()
        {
            return new TaskPadView();
        }

        /// <summary>
        /// 0: 最近1周 1： 最近1月 2：全部
        /// </summary>
        /// <param name="type"></param>
        public void RefleshData(int type)
        {
            int Days = 0;

            switch (type)
            {
                case 0:
                    Days = 7;
                    break;
                case 1:
                    Days = 30;
                    break;
                case 2:
                    Days = 36500;
                    break;
            }

            PrepareTask.Clear();
            ProgressTask.Clear();
            FinishingTask.Clear();
            OutofDateTask.Clear();

            if (Model == null)
            {
                return;
            }

            foreach (Task i in Model.LeafTasks)
            {
                
                if (i.EndDT - DateTime.Now < new TimeSpan(Days, 0, 0, 0)
                    && (i.TaskStatus == TaskStatus.InProgress ))
                {
                    FinishingTask.Add(i);
                }
                else if (DateTime.Now - i.EndDT > new TimeSpan(0)
                    && (i.TaskStatus == TaskStatus.InProgress || i.TaskStatus == TaskStatus.Prepare))
                {
                    OutofDateTask.Add(i);
                }
                else if (i.StartDT - DateTime.Now < new TimeSpan(Days, 0, 0, 0) && i.TaskStatus == TaskStatus.Prepare)
                {
                    PrepareTask.Add(i);
                }
                else if (i.EndDT - i.StartDT > new TimeSpan(0) && i.TaskStatus == TaskStatus.InProgress)
                {
                    ProgressTask.Add(i);
                }

            }
        }
    }
}
